<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/7
 * Time: 14:25
 */

namespace zhtfrontend\controllers;

use common\models\datas\Brand;
use common\models\datas\Cate;
use common\models\datas\CateAttr;
use common\models\datas\Type;
use yii\widgets\ActiveForm;
use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\Coupons;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\BadRequestHttpException;
use yii\helpers\ArrayHelper;
use yii\data\Pagination;
use yii\helpers\Json;

class CouponsController extends BaseController
{


    // public $layout='@app/views/layouts/iframe.php';

    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
        $coupons = Coupons::find()->orderBy('id DESC');
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['name'])) {
                $coupons->andWhere(['like', 'name', $search_data['name']]);
            }
            if (!empty($search_data['total'])) {
                $coupons->andWhere(['total' => $search_data['total']]);
            }
            if (!empty($search_data['type'])) {
                $coupons->andWhere(['like', 'type', $search_data['type']]);
            }
        }
        $pages = new Pagination(['totalCount' => $coupons->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $coupons = $coupons->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($coupons, ['operate']);

        return $this->render('index', ['data' => $array, 'pages' => $pages]);
    }
    /*
     * 优惠券添加
     */
//    public function actionCreate(){
//        $model=new Coupons();
//        $model->scenario='createcoupons';
//        $type=Type::find()->where(['is_delete'=>0])->all();
//        $brand=Brand::find()->all();
//        $site_id=Fun::getSiteId();
//        $cate_attr=CateAttr::find()->select(['cat_id'])->where(['site_id'=>$site_id])->orderBy('pc_sort asc')->column();
//        $category=Cate::find()->where(['in','id',$cate_attr])->andWhere(['is_child'=>0,'is_delete'=>0])->indexBy('id')->asArray()->all();
//        $tree=ArrayHelper::map($category,'id','name');
//        $type_array=ArrayHelper::map($type,'id','name');
//        $brand_array=ArrayHelper::map($brand,'id','name');
//        if($model->load(\Yii::$app->request->post()) && $model->validate()){
//            if(isset(\Yii::$app->request->post()['array'])){
//                $model->conditions=serialize(['key'=>$model->conditions,'value'=>\Yii::$app->request->post()['array'][$model->conditions]]);
//            }else{
//                $model->conditions=serialize(['key'=>$model->conditions]);
//            }
//            $model->platforms=serialize($model->platforms);
//            $model->start_time=strtotime($model->start_time);
//            $model->end_time=strtotime($model->end_time);
//            if($model->save(false)){
//                return $this->redirectMessage('添加成功',Fun::url(['coupons/index']));
//            }
//        }
//        return $this->render('create',['model'=>$model,'type_array'=>$type_array,'brand_array'=>$brand_array,'tree'=>$tree]);
//    }

    /*
     * 优惠券添加
     * 2017-5-8
     * */
    public function actionCreate()
    {
        $model = new Coupons();
        $model->scenario = 'createcoupons';
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->platforms = serialize(['0' => 'pc', '1' => 'wechat', '2' => 'app']);
            $model->start_time = strtotime($model->start_time);
            $model->end_time = strtotime($model->end_time);
            $model->user_id = \Yii::$app->user->id;
            if ($model->maturity_type == 'W') {
                $model->maturity_value = $model->maturity_week;
            } else {
                $model->maturity_value = $model->maturity_day;
            }
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['coupons/index']));
            }
        }
        return $this->render('create', ['model' => $model]);
    }
    /*
     * 优惠券修改
     * */
//    public function actionUpdate(){
//        if(\Yii::$app->request->get('id')==null){
//            throw new BadRequestHttpException(\Yii::t('frontend','信息不存在'));
//        }
//        $model=Coupons::findOne(\Yii::$app->request->get('id'));
//        $model->scenario='updatecoupons';
//        if($model==null){
//            throw new NotFoundHttpException(\Yii::t('frontend','信息不存在'));
//        }
//        $array=unserialize($model->conditions);
//        $model->platforms=unserialize($model->platforms);
//        $model->conditions=$array['key'];
//        $model->start_time=\Yii::$app->formatter->asDatetime($model->start_time);
//        $model->end_time=\Yii::$app->formatter->asDatetime($model->end_time);
//        $checked_values=isset($array['value'])?$array['value']:'';
////        $type=Type::find()->where(['is_delete'=>0])->all();
////        $brand=Brand::find()->all();
//        $site_id=Fun::getSiteId();
////        $cate_attr=CateAttr::find()->select(['cat_id'])->where(['site_id'=>$site_id])->orderBy('pc_sort asc')->column();
////        $category=Cate::find()->where(['in','id',$cate_attr])->andWhere(['is_child'=>0,'is_delete'=>0])->indexBy('id')->asArray()->all();
////        $tree=ArrayHelper::map($category,'id','name');
////        $type_array=ArrayHelper::map($type,'id','name');
////        $brand_array=ArrayHelper::map($brand,'id','name');
//        if($model->load(\Yii::$app->request->post()) && $model->validate()){
//            if(isset(\Yii::$app->request->post()['array'])){
//                $model->conditions=serialize(['key'=>$model->conditions,'value'=>\Yii::$app->request->post()['array'][$model->conditions]]);
//            }else{
//                $model->conditions=serialize(['key'=>$model->conditions]);
//            }
//            $model->platforms=serialize($model->platforms);
//            $model->start_time=strtotime($model->start_time);
//            $model->end_time=strtotime($model->end_time);
//            if($model->save(false)){
//                return $this->redirectMessage('修改成功',Fun::url(['coupons/index']));
//            }
//        }
//        return $this->render('create',['model'=>$model,'checked_values'=>$checked_values]);
//    }

    /*
     * 优惠券修改
     * 2017-5-8
     * */
    public function actionUpdate()
    {
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('frontend', '信息不存在'));
        }
        $model = Coupons::findOne(\Yii::$app->request->get('id'));
        $model->scenario = 'updatecoupons';
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('frontend', '信息不存在'));
        }
        if ($model->maturity_type == 'W') {
            $model->maturity_week = $model->maturity_value;
        } else {
            $model->maturity_day = $model->maturity_value;
        }
        $model->platforms = unserialize($model->platforms);
        $model->start_time = \Yii::$app->formatter->asDatetime($model->start_time);
        $model->end_time = \Yii::$app->formatter->asDatetime($model->end_time);
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->platforms = serialize(['0' => 'pc', '1' => 'wechat', '2' => 'app']);
            $model->start_time = strtotime($model->start_time);
            $model->end_time = strtotime($model->end_time);
            if ($model->maturity_type == 'W') {
                $model->maturity_value = $model->maturity_week;
            } else {
                $model->maturity_value = $model->maturity_day;
            }
            if ($model->save(false)) {
                return $this->redirectMessage('修改成功', Fun::url(['coupons/index']));
            }
        }
        return $this->render('create', ['model' => $model]);
    }
}
